package com.atguigu.controller;

import com.atguigu.entity.BaseBrand;
import com.atguigu.entity.TOrder;
import com.atguigu.entity.TOrderDetail;
import com.atguigu.login.TingShuLogin;
import com.atguigu.mapper.TOrder1Mapper;
import com.atguigu.result.RetVal;
import com.atguigu.service.BaseBrandService;
import com.atguigu.service.TOrder1Service;
import com.atguigu.service.TOrderDetail1Service;
import com.atguigu.vo.OrderInfoVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.UUID;

/**
 * <p>
 * 订单减免表 前端控制器
 * </p>
 *
 * @author 强哥
 * @since 2023-12-16
 */
@Tag(name = "shardingsphere测试")
@RestController
@RequestMapping("api/sharding")
public class MyOrderController {
    @Autowired
    private BaseBrandService brandService;
    @Autowired
    private TOrder1Service order1Service;
    @Autowired
    private TOrderDetail1Service detail1Service;
    @Operation(summary = "查询所有的品牌信息")
    @GetMapping("getAllBrand")
    public List<BaseBrand> getAllBrand()  {
        return brandService.list();
    }
    @Operation(summary = "保存品牌信息")
    @PostMapping("saveBrand")
    public String saveBrand(@RequestBody BaseBrand baseBrand)  {
        brandService.save(baseBrand);
        return "success";
    }
    @Operation(summary = "单表分库分表保存订单")
    @GetMapping("test01/{loopNum}")
    public String saveOrderInfo(@PathVariable Integer loopNum)  {
        for (int i = 0; i < loopNum; i++) {
            TOrder tOrder = new TOrder();
            tOrder.setOrderPrice(99);
            String uuid = UUID.randomUUID().toString();
            tOrder.setTradeNo(uuid);
            tOrder.setOrderStatus("未支付");
            //分库分表的依据
            int userId=new Random().nextInt(20);
            tOrder.setUserId(Long.parseLong(userId+""));
            System.out.println("用户id:"+userId);
            order1Service.save(tOrder);
        }
        return "success";
    }
    @Operation(summary = "多表分库分表保存订单与订单详情")
    @GetMapping("test02/{userId}")
    public String saveOrderAndDetail(@PathVariable Long userId)  {
        TOrder tOrder = new TOrder();
        tOrder.setTradeNo("enjoy6288");
        tOrder.setOrderPrice(9900);
        tOrder.setUserId(userId);
        order1Service.save(tOrder);

        TOrderDetail iphone13 = new TOrderDetail();
        iphone13.setOrderId(tOrder.getId());
        iphone13.setSkuName("Iphone13");
        iphone13.setSkuNum(1);
        iphone13.setSkuPrice(6000);
        iphone13.setUserId(userId);
        detail1Service.save(iphone13);

        TOrderDetail sanxin = new TOrderDetail();
        sanxin.setOrderId(tOrder.getId());
        sanxin.setSkuName("三星");
        sanxin.setSkuNum(2);
        sanxin.setSkuPrice(3900);
        sanxin.setUserId(userId); //要进行分片计算
        detail1Service.save(sanxin);
        System.out.println("执行完成");
        return "success";
    }
    @Autowired
    private TOrder1Mapper order1Mapper;

    @Operation(summary = "查询订单与订单详情")
    @GetMapping("test03/{userId}")
    public String queryOrderAndDetail(@PathVariable Long userId)  {
        List<TOrder> orderList=order1Mapper.queryOrderAndDetail(userId,null);
        //System.out.println(orderList);
        //使用order_id查询 如果没有使用分片的键查询 跨库跨表查询 速度相当慢
        //List<TOrder> orderList=order1Mapper.queryOrderAndDetail(null,1736949391278764033L);
        System.out.println(orderList);
        return "success";
    }
}
